package com.liik.bomb.screen.action;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;

import com.liik.game.mvc.object.Menu.Item;
import com.liik.game.mvc.tiled.TiledGameRenderer;
import com.liik.game.mvc.tiled.object.Entity;
import com.liik.game.mvc.tiled.object.Tile;

public class SimpleBombGameRenderer extends TiledGameRenderer {

	protected Paint waterColor, bridgeColor, emptyColor, earthColor, wallColor, heroColor, menuBGColor, menuColor, bombColor, explosionColor;

	public SimpleBombGameRenderer() {
		waterColor = new Paint();
		waterColor.setARGB(255, 0, 0, 128);
		bridgeColor = new Paint();
		bridgeColor.setARGB(255, 64, 0, 128);
		emptyColor = new Paint();
		emptyColor.setARGB(255, 128, 0, 128);
		earthColor = new Paint();
		earthColor.setARGB(255, 128, 10, 255);
		wallColor = new Paint();
		wallColor.setARGB(255, 30, 30, 30);
		heroColor = new Paint();
		heroColor.setARGB(255, 255, 255, 255);
		menuBGColor = new Paint();
		menuBGColor.setARGB(90, 100, 0, 0);
		menuColor = new Paint();
		menuColor.setARGB(255, 255, 255, 0);
		bombColor = new Paint();
		bombColor.setARGB(255, 0, 0, 0);
		explosionColor = new Paint();
		explosionColor.setARGB(100, 255, 0, 0);
	}
	
	@Override
	protected void renderTile(Tile tile, Canvas canvas, float x, float y, float width, float height) {
		Paint paint = wallColor;

		if (tile != null) {
			switch (tile.type) {
			case Bridge:
				paint = bridgeColor;
				break;
			case Earth:
				paint = earthColor;
				break;
			case Empty:
				paint = emptyColor;
				break;
			case Wall:
				paint = wallColor;
				break;
			case Water:
				paint = waterColor;
				break;
			}
		}
		canvas.drawRect(x + 1, y + 1, x + width, y + height, paint);
	}

	@Override
	protected void renderMenuItem(Item item, Canvas canvas) {
		canvas.drawRect(item.region.x, item.region.y, item.region.x + item.region.width, item.region.y + item.region.height, menuBGColor);
		canvas.drawText(item.caption, item.region.x + 2, item.region.y + item.region.height / 2 , menuColor);
	}

	@Override
	protected void renderEntity(Entity entity, Canvas canvas, float x, float y, float width, float height) {
		if (entity instanceof BombermanEntity) {
			renderBomberman(entity, canvas, x, y, width, height);
		} else if (entity instanceof BombEntity) {
			renderBomb(entity, canvas, x, y, width, height);
		} else if (entity instanceof EnemyEntity) {
			renderEnemy(entity, canvas, x, y, width, height);
		} else if (entity instanceof ItemEntity) {
			renderItem(entity, canvas, x, y, width, height);
		} else {
			canvas.drawRect(x, y, x + width, y + height, menuBGColor);
			canvas.drawText("Entity", x + 2, y + (height / 2) , menuColor);
		}		
	}

	private void renderItem(Entity entity, Canvas canvas, float x, float y, float width, float height) {
		canvas.drawRect(x, y, x + width, y + height, menuBGColor);
		canvas.drawText("Item", x + 2, y + (height / 2) , menuColor);		
	}

	private void renderEnemy(Entity entity, Canvas canvas, float x, float y, float width, float height) {
		canvas.drawRect(x, y, x + width, y + height, menuBGColor);
		canvas.drawText("Enemy", x + 2, y + (height / 2) , menuColor);
	}

	private void renderBomb(Entity entity, Canvas canvas, float x, float y, float width, float height) {
		BombEntity bomb = (BombEntity) entity;
		switch (bomb.state) {
		case Counting:
			canvas.drawCircle(x + (width/2), y + (width/2), 6, bombColor);
			break;
		case Done:
			break;
		case Exploding:
			canvas.drawCircle(x + (width/2), y + (width/2), width / 2, explosionColor);
			for (int i = 1; i <= bomb.rangeEast; i++) {
				canvas.drawCircle(x + (width/2) + (i * width), y + (width/2), width / 2, explosionColor);
			}
			for (int i = 1; i <= bomb.rangeWest; i++) {
				canvas.drawCircle(x + (width/2) - (i * width), y + (width/2), width / 2, explosionColor);
			}
			for (int i = 1; i <= bomb.rangeNorth; i++) {
				canvas.drawCircle(x + (width/2), y + (width/2) - (i * width), width / 2, explosionColor);
			}
			for (int i = 1; i <= bomb.rangeSouth; i++) {
				canvas.drawCircle(x + (width/2), y + (width/2) + (i * width), width / 2, explosionColor);
			}
			break;
		}		
	}

	private void renderBomberman(Entity entity, Canvas canvas, float x, float y, float width, float height) {
		canvas.drawCircle(x + (width / 2), y + (height / 2), 8, heroColor);		
	}

	@Override
	protected void renderNavigationTarget(Canvas canvas, float x, float y) {
		canvas.drawCircle(x, y, 3, heroColor);
	}

	@Override
	public void setContext(Context context) {
		// TODO Auto-generated method stub
		
	}
}
